package Servlet;

import Bean.Employee;
import com.alibaba.fastjson.JSONObject;
import netscape.javascript.JSObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		String empno=req.getParameter("empno");
		String ename=req.getParameter("ename");
		String job=req.getParameter("job");
		String hiredateStr=req.getParameter("hiredate");
		String sal=req.getParameter("sal");
		String deptno=req.getParameter("deptno");

		Date hiredate=new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		try {
			hiredate=format.parse(hiredateStr);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

		Employee employee=new Employee(Integer.parseInt(empno),ename,job,
				hiredate,Double.parseDouble(sal),Integer.parseInt(deptno));
		String str= JSONObject.toJSONString(employee);
		System.out.println(employee);
		resp.getWriter().write("员工对象 "+employee+"添加成功");
		resp.getWriter().write(str);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
